package com.mls.fivedistrict.controller.backend;

import com.mls.fivedistrict.common.PageFilter;
import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.controller.BaseController;
import com.mls.fivedistrict.exception.CustomerException;
import com.mls.fivedistrict.pojo.OrePool;
import com.mls.fivedistrict.server.OrePoolService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Optional;

/**
 * Created by chenxiuxiang on 2018/4/1.
 */
@RestController
@RequestMapping("/api/manager")
public class OrePoolController extends BaseController {

    @Resource(name = "orePoolService")
    OrePoolService orePoolService;


    @PostMapping("/v1/orePool")
    public ServiceResponse addOrePool(@RequestBody OrePool orePool) {
        Optional.ofNullable(orePool.getIntrgralId()).orElseThrow(() -> new CustomerException("积分不存在"));
        Optional.ofNullable(orePool.getStartDate()).orElseThrow(() -> new CustomerException("开始日期不存在"));
        Optional.ofNullable(orePool.getEndDate()).orElseThrow(() -> new CustomerException("结束日期不存在"));
        Optional.ofNullable(orePool.getTotal()).orElseThrow(() -> new CustomerException("总数不存在"));
        return orePoolService.addOrePool(orePool);
    }


    @GetMapping("/v1/orePools")
    public ServiceResponse getPools(HttpServletRequest httpServletRequest) {
        PageFilter pageFilter = getPageFilter();
        return orePoolService.getOrePoolList(pageFilter);
    }


}
